﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="ComplexTypeWithThreeDifferentArrayTypes", Namespace="http://kis.x-road.eu/", Target=typeof(ComplexTypeWithThreeDifferentArrayTypes))]
    public class ComplexTypeWithThreeDifferentArrayTypesSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ComplexTypeWithThreeDifferentArrayTypes clazz;
        
        public ComplexTypeWithThreeDifferentArrayTypesSerializer(ComplexTypeWithThreeDifferentArrayTypes clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("Builtins");
            if (clazz.Builtins!=null) {
                for (int x = 0; ((clazz.Builtins != null) 
                            && (x < clazz.Builtins.Count)); x = (x + 1)) {
                    if (clazz.Builtins[x]!=null) {
                        Writer.WriteStartElement("item");
                        Writer.WriteString(new global::Xtee.Core.Schema.XsdDateTime(((System.DateTime)(clazz.Builtins[x]))).ToString());
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Udfs");
            if (clazz.Udfs!=null) {
                for (int x = 0; ((clazz.Udfs != null) 
                            && (x < clazz.Udfs.Count)); x = (x + 1)) {
                    if (clazz.Udfs[x]!=null) {
                        Writer.WriteStartElement("item");
                        Writer.WriteString(clazz.Udfs[x].ToString());
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Complexs");
            if (clazz.Complexs!=null) {
                for (int x = 0; ((clazz.Complexs != null) 
                            && (x < clazz.Complexs.Count)); x = (x + 1)) {
                    if (clazz.Complexs[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaComplexs = this.GetMetaData(clazz.Complexs[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.Complexs[x].GetType())) {
                            Writer.WriteTypeAttribute(metaComplexs.TypeQualifiedName.Namespace, metaComplexs.TypeQualifiedName.Name);
                        }
                        var complexsSerializer = metaComplexs.NewSerializer(clazz.Complexs[x]);
                        complexsSerializer.WriteXmlAttributes(Writer);
                        complexsSerializer.WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="Builtins") {
                if ((Reader.IsElementNil() == false)) {
                    clazz.Builtins = new System.Collections.Generic.List<System.DateTime?>();
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Builtins");
                        for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                    && (Reader.LocalName == "item")); ) {
                            if ((Reader.IsElementNil() == false)) {
                                clazz.Builtins.Add(new global::Xtee.Core.Schema.XsdDateTime(Reader.ReadElementString("item")).Value);
                            }
                            else {
                                clazz.Builtins.Add(null);
                                Reader.Skip();
                            }
                        }
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeDifferentArrayTypes>Builtins", Reader.LocalName);
            }
            if (Reader.LocalName=="Udfs") {
                if ((Reader.IsElementNil() == false)) {
                    clazz.Udfs = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Gender>();
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Udfs");
                        for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                    && (Reader.LocalName == "item")); ) {
                            if ((Reader.IsElementNil() == false)) {
                                clazz.Udfs.Add(new global::Kis.Xroad.Eu.Gender(Reader.ReadElementString("item")));
                            }
                            else {
                                clazz.Udfs.Add(null);
                                Reader.Skip();
                            }
                        }
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeDifferentArrayTypes>Udfs", Reader.LocalName);
            }
            if (Reader.LocalName=="Complexs") {
                if ((Reader.IsElementNil() == false)) {
                    clazz.Complexs = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Employee>();
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Complexs");
                        for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                    && (Reader.LocalName == "item")); ) {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaComplexs = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                                global::Kis.Xroad.Eu.Employee item = metaComplexs.NewEntity<global::Kis.Xroad.Eu.Employee>();
                                clazz.Complexs.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    var complexsSerializer = metaComplexs.NewSerializer(item);
                                    complexsSerializer.ReadXmlAttributes(Reader);
                                    Reader.ReadStartElement("item");
                                    complexsSerializer.ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Complexs.Add(null);
                                Reader.Skip();
                            }
                        }
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeDifferentArrayTypes>Complexs", Reader.LocalName);
            }
        }
    }
}
